﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace VPN_Emulator
{
    //! Klasa reprezentująca okno wyników końcowych laboratorium
    public partial class Results : Form
    {
        private List<Router> routerList;

        //! Konstruktor
        public Results()
        {
            InitializeComponent();           
        }

        //! Lista routerów w sieci
        public List<Router> RouterList
        {
            get { return routerList; }
            set { routerList = value; }
        }

        //! Wyliczenie i wyświetlenie rezultatu
        public void getResult()
        {
            float all = 0;
            float correct = 0;

            foreach (Router r in routerList)
            {
                for (int i = 0; i < r.TestCaseList.Count; i++)
                {
                    
                    for (int ee = 0; ee < r.TestCaseList.Count; ee++)
                    {
                        foreach (KeyValuePair<int, string> attr in r.TestCaseList[ee].getExpectedAttr())
                        {
                            if (r.TestCaseList[ee].Correct == true)
                            {
                                all++;
                                if (r.TestCaseList[ee].Result == true) correct++;
                            }

                        }
                    }
                }
            }

            float result = correct/all  * 10;
            textBox1.Text = result.ToString() + "/10";

        }

        //! Zmiana podglądanego routera
        private void routersComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            dataGridView2.Rows.Clear();
            
            for (int i = 0; i < routerList[routersComboBox.SelectedIndex].TestCaseList.Count; i++)
            {
                dataGridView1.Rows.Add();
                dataGridView1[0, i].Value = i;
                dataGridView1[1, i].Value = routerList[routersComboBox.SelectedIndex].TestCaseList[i].GetType;
                dataGridView1[2, i].Value = routerList[routersComboBox.SelectedIndex].TestCaseList[i].Oid;
                dataGridView1[3, i].Value = routerList[routersComboBox.SelectedIndex].TestCaseList[i].Description;

                bool correct = true;

                for(int ee=0; ee<routerList[routersComboBox.SelectedIndex].TestCaseList.Count;ee++)
                {
                    foreach (KeyValuePair<int, string> attr in routerList[routersComboBox.SelectedIndex].TestCaseList[ee].getExpectedAttr())
                    {
                        if (routerList[routersComboBox.SelectedIndex].TestCaseList[ee].Correct == true)
                        {
                            if (routerList[routersComboBox.SelectedIndex].TestCaseList[ee].Result == false) correct = false;
                        }
                     
                    }
                }

                if(correct==true) dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Green;
                else dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;


                
            }
            dataGridView1.ClearSelection();
            dataGridView2.ClearSelection();
        }

        //! Zmiana podglądanego testu
        private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            dataGridView2.Rows.Clear();
            int j = 0;

            foreach (KeyValuePair<int, string> attr in routerList[routersComboBox.SelectedIndex].TestCaseList[e.RowIndex].getExpectedAttr())
            {
               
                dataGridView2.Rows.Add();
                if (routerList[routersComboBox.SelectedIndex].TestCaseList[e.RowIndex].Correct == true)
                {
                    if (routerList[routersComboBox.SelectedIndex].TestCaseList[e.RowIndex].ResultAttr[j] == true) dataGridView2.Rows[j].DefaultCellStyle.BackColor = Color.Green;
                    else dataGridView2.Rows[j].DefaultCellStyle.BackColor = Color.Red;
                }
                dataGridView2[0, j].Value = attr.Key;
                dataGridView2[1, j].Value = attr.Value;
                j++;
            }
            dataGridView2.ClearSelection();
        }


        //! Załadowanie listy routerów
        private void Results_Load(object sender, EventArgs e)
        {
            routersComboBox.Items.Clear();
            foreach (Router r in routerList)
            {
                routersComboBox.Items.Add(r.Name);
            }
        }

        //! Zakończenie laboratorium
        private void close_Click(object sender, EventArgs e)
        {
            
       
            if (MessageBox.Show("Czy na pewno chcesz zakończyć laboratorium i zamknąć program?", "Kończenie pracy", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) return;
            else Application.Exit();
        
        }
    }
}
